# Chapter 9. CGI Programming

#### Contents:

What Is CGI?
HTML Forms
The CGI Specification
Important Considerations for CGI Scripts
CGI and Databases

Including a section on CGI within a book about databases may seem a bit like having a chapter on automotive repair in a cookbook: sure, you need a running car to go to the grocery, but isn't it a bit out of place? While it is true that the whole of CGI and web programming in general is vastly out of the scope of this book, a short primer is all that is needed to extend the capabilities of MySQL and mSQL to the high-visibility realm of the World Wide Web.

This chapter is primarily intended for those people who want to learn about databases but would not mind trying a little web programming. If your last name is Berners-Lee or Andressen you probably will not find anything here you do not already know. Even if you are not new to the world of CGI, it could be useful to keep a short reference handy while you ponder the mysteries of MySQL and mSQL.

## 9.1. What Is CGI?

As with most names that have acronyms, Common Gateway Interface ( CGI) tells us very little about what it really means. What are we interfacing with? Where is this gateway? What's so common about it anyway? To answer these questions, we need to take a step back and look at the World Wide Web as a whole.

Tim Berners-Lee, a physicist at CERN, invented the web in 1990 (with planning going as far back as 1988). The idea was to give particle physicists the ability to exchange multimedia information -- text, graphics, sounds -- quickly and easily over the Internet. The World Wide Web had three major components: HTML, URLs, and HTTP. HTML is the formatting language used to display content on the web. URLs are the addresses used to retrieve the HTML (and other) content from a web server. Finally, HTTP is the language spoken by the web server, giving clients the ability to ask the server for documents.

While the ability to send content of all sorts over the Internet was revolutionary, another possibility was quickly realized. If it was possible to send any text through the web, why shouldn't it be possible to send text that does not come from a file, but rather is generated by a program? This opened up a whole realm of possibilities. As a simple example, a program that outputs the current time could be used, so that the reader sees the correct time every time he views the page. A few bright fellows at the National Center for Supercomputing Applications (NCSA) who were writing a web server saw the potential of this and CGI was soon born.

CGI is the set of rules by which programs on the server can send information via the web server to the client. The CGI specification was also accompanied by changes to HTML and HTTP to introduce a new feature known as forms.

Where CGI allows programs to send output to a client, forms extend that capability by allowing the client to send information to the CGI program. Now the user can not only view the current time, but he or she can also set the clock! Forms and CGI opened the door to true interactivity on the Internet. Popular applications of CGI include:

- Dynamic HTML. Entire sites of pages can be generated from a single CGI program.

- Search engines that retrieve documents that match keywords chosen by the user.

- Guestbooks and bulletin boards for users to add their own comments to a site.

- Order forms.

- Surveys.

- Information retrieval that serves up details from a database.

Over the next few chapters, we will discuss each of these CGI applications along with a couple of others. More specifically, all of these applications offer a perfect opportunity to combine CGI with a database. This topic is what interests us in this section.

Copyright © 2001 O'Reilly & Associates. All rights reserved.